// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Liberate hotage
// -----------------------------------------------

private [
	"_arrayofpos",
	"_missioncomplete",
	"_position",
	"_unit",
	"_vehicle",
	"_vehicle2",
	"_group"
];

_unit = _this select 0;

//A3 TODO: setVehicleInit
_unit setVehicleInit "this addAction ['<t color=''#ffcb00''>Follow me</t>', 'warcontext\actions\WC_fnc_dofollowme.sqf', [], 6, false, true];";
processInitCommands;

_arrayofpos = [];
_missioncomplete = false;

_unit setVariable ["wcprotected", true];
_unit setCaptive true;
_unit allowFleeing 0;
_unit setUnitPos "Up";
removeAllWeapons _unit;

_arrayofpos = [position _unit, "all"] call WC_fnc_gethousespositions;
_position = _arrayofpos call BIS_fnc_selectRandom;

_unit setPos _position;
_unit setDamage 0;
_unit playMoveNow "AmovPercMstpSnonWnonDnon_AmovPercMstpSsurWnonDnon";
_unit setVariable ["wchostage", true, true];
_unit stop true;

_group = createGroup east;
_vehicle = _group createUnit [(wcspecialforces call BIS_fnc_selectRandom), position _unit, [], 1, "NONE"];
_vehicle2 = _group createUnit [(wcspecialforces call BIS_fnc_selectRandom), position _unit, [], 8, "NONE"];

_vehicle allowDammage false;
_vehicle2 allowDammage false;

wcgarbage = [_group] spawn WC_fnc_grouphandler;
wcgarbage = [_group, (position (leader _group)), 30] spawn WC_fnc_patrol;

wcgarbage = [_vehicle] spawn WC_fnc_dosillything;
wcgarbage = [_vehicle2] spawn WC_fnc_dosillything;

sleep 10;

_vehicle allowDammage true;
_vehicle2 allowDammage true;

wcgarbage = [_unit] spawn {
	private ["_unit"];
	_unit = _this select 0;
	while {((alive _unit) and (_unit getVariable "wchostage"))} do {
		wchostage = _unit;
		["wchostage", "client"] call WC_fnc_publicvariable;
		sleep (5 + random 15);
	};
};

while {!_missioncomplete} do {
	if ((!alive _unit) or (damage _unit > 0.7)) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONFAILED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
	};
	if ((getMarkerPos "respawn_west") distance _unit < 100) then {
		_unit setVariable ["wchostage", false, true];
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONCOMPLETED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
		wcleveltoadd = 1;
	};
	sleep 1;
};

sleep 120;

_unit setDamage 1;
_vehicle setDamage 1;
_vehicle2 setDamage 1;

deleteVehicle _unit;
deleteVehicle _vehicle;
deleteVehicle _vehicle2;